package com.matt.controller.web.cookbook;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.simpleCookbook.SimpleCookbook;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.cookbook.SimpleCookbookService;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

import static com.matt.foundation.constant.RequestConstant.TICKET;
import static com.matt.foundation.model.ErrorCode.PARAM_CAN_NOT_BLANK;

@RestController
@RequestMapping("/web/cookbook")
@Slf4j
public class WebSimpleCookbookController {

	@Autowired
	private MemVerificationCache cache;

	@Autowired
	private SsoCallService ssoCallService;

	@Autowired
	SimpleCookbookService baseService;

	@ApiOperation(value = "创建一个简易食谱")
	@PostMapping("/simple")
	public StatusResponse add(@RequestHeader(TICKET) String ticket,
							  String title, String content,
							  Date beginAt, Date endAt,
							  MultipartFile file) throws IOException {

		StaffDTO staff = cache.getStaff(ticket);

		SimpleCookbook simpleCookbook = baseService.add(staff, title, content, file, beginAt, endAt);

		return StatusResponse.success(simpleCookbook);
	}


	@ApiOperation(value = "获取简易食谱列表")
	@GetMapping("/simples")
	public StatusResponse list(@RequestHeader(TICKET) String ticket, PagerQuery pagerQuery){
		StaffDTO staff = cache.getStaff(ticket);

		Page<SimpleCookbook> page = baseService.page(staff.getAreaId(), pagerQuery);

		return StatusResponse.success(page);
	}


	@ApiOperation(value = "获取一个简易食谱对象")
	@GetMapping("/simple/{id}")
	public StatusResponse get(@RequestHeader(TICKET) String ticket, @PathVariable String id){
		StaffDTO staff = cache.getStaff(ticket);

		SimpleCookbook ret = baseService.get(staff, id);

		return StatusResponse.success(ret);
	}

	@ApiOperation(value = "逻辑删除")
	@DeleteMapping("/simple/{id}")
	public StatusResponse delete(@RequestHeader(TICKET) String ticket, @PathVariable String id){
		StaffDTO staff = cache.getStaff(ticket);
		baseService.deleteSimpleCookbook(staff, id);
		return StatusResponse.success(true);
	}

	@ApiOperation(value = "逻辑删除多个")
	@DeleteMapping("/simple")
	public StatusResponse delete(@RequestHeader(TICKET) String ticket, String[] ids){
		StaffDTO staff = cache.getStaff(ticket);
		if (ids == null || ids.length == 0){
			throw new BusinessException(PARAM_CAN_NOT_BLANK);
		}

		for (String id : ids) {
			baseService.deleteSimpleCookbook(staff, id);
		}

		return StatusResponse.success(true);
	}

	@ApiOperation(value = "通知家长")
	@PostMapping("/simple/notify")
	public StatusResponse notify(@RequestHeader(TICKET) String ticket, String id){
		return StatusResponse.success(true);
	}

}
